<?php
namespace app\common\controller;

use think\Controller;
use think\Db;
use think\Config;

class Auth extends Controller
{
    function __construct()
    {
        parent:: __construct();
        $acc_domain = $_SERVER['HTTP_HOST'];
		$web_domain_list = Db::name('weblist')
			->where('domain', '=', $acc_domain)
			->whereOr('domain2', '=', $acc_domain)
			->find();
		$web_cofig = Db::name('weblist')
			->where('web_id', '=', 1)
			->find();
		$web_config_url = 'http://'.$web_cofig['domain'].'/';
		if ($web_domain_list) {
			define('WEB_ID', $web_domain_list['web_id']);
			define('PREFIX', $web_domain_list['prefix']);
			define('WEB_KEY', md5($web_domain_list['web_key']));
			define('RUN_KEY', md5(config('database')['username'] . md5(config('database')['password'])));
			$config = Db::table($web_domain_list['prefix'] . 'configs')->select();
			foreach ($config as $row) {
				config("sys_{$row['k']}", $row['v']);
			}
			foreach ($web_domain_list as $key => $val) {
				config("web_{$key}", $val);
			}
		} else {
			$this->error('此站点未开通',$web_config_url);
		}
		//判断站点是否过期
		if (WEB_ID != 1 && strtotime($web_domain_list['end_time']) < time()) {
			$this->error('该站点已经过期',$web_config_url);
		}
		if (WEB_ID != 1 && $web_domain_list['status'] != 1) {
			$this->error('该站点已经封禁',$web_config_url);
		}
    }
}